<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://yui.yahooapis.com/3.4.1/build/yui/yui-min.js"></script>
<script type="text/javascript">
YUI().use("node",function(Y) {
	var myt = Y.one('#myt1');
	Y.on('click',function(e){
		e.preventDefault();
		var r = document.selection.createRange();//当前光标的位置。
		var dr = r.duplicate();//这时候的dr和r一致
		dr.moveToElementText(myt.getDOMNode()); //包含全部文字 |abcde|
		dr.setEndPoint('EndToEnd',r);//将dr的结束点从最后移到光标所在的位置。
		alert(dr.text.length - r.text.length);//r的text是空，所以就是文字的长度。
		dr.collapse(false);//说明collapse默认是从end合并到start
		myt.focus();
		r = document.selection.createRange();
		r.moveToElementText(myt.getDOMNode());
		r.collapse(false);
		r.select();
	},'#b');
});

</script>
</head>
<body>
<p>真的吗？</p>
<form>
<textarea rows="" cols="" id="myt1" tabindex="1">abcde</textarea>
<button id="b">测试</button>
</form>
		<textarea id="someTextArea" 
		rows="3" cols="40">Some initial text to play with</textarea>
		<input type="button" value="Erase Last Word" 
		onclick="eraseLastWord();" />
		<script type="text/javascript">
			// Function to demonstrate the use of moveStart by erasing
			// the last word in "someTextArea"  
			//
			// Note that this example function does not work well with 
			// punctuation.
			function eraseLastWord() {				
				var theTextArea = document.getElementById("someTextArea");
				var theTextRange = theTextArea.createTextRange();

				// Select the last word in the TextRange by advancing 
				// the Start pointer of the TextRange to the end and then
				// moving back one word
				theTextRange.moveStart("textedit");
				theTextRange.moveStart("word", -1);

				// Now that the last word of the TextArea is selected in 
				// the text range, erase it
				theTextRange.text = ""; 			
			}
		</script>	
</body>
</html>